package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.ProductAddDTO;
import org.jsola.hr.dto.ProductUpdateDTO;
import org.jsola.hr.entity.ProductBannerDO;
import org.jsola.hr.entity.ProductDO;
import org.jsola.hr.query.ProductQuery;
import org.jsola.hr.service.IProductBannerService;
import org.jsola.hr.service.IProductService;
import org.jsola.hr.vo.ProductListVO;
import org.jsola.hr.vo.ProductVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;
import javax.validation.Valid;


/**
 * 产品
 *
 * @author zhr
 */
@RestController("hrProductController")
@RequestMapping(HrConstants.API_PATH + "/v1/products")
public class ProductController implements IProductControllerAPI {

    @Autowired
    private IProductService productService;

    @Autowired
    private IProductBannerService productBannerService;

    @Override
    @PostMapping
    public Result<ProductVO> save(@Valid @RequestBody ProductAddDTO productAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增产品权限校验

        return Result.success(
                productService.save(productAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody ProductUpdateDTO productUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改产品权限校验

        int count = productService.update(productUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{productId}")
    public Result<Integer> delete(@PathVariable String productId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除产品权限校验

        int count = productService.deleteByIds(tokenUser, productId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] productIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除产品权限校验

        int count = productService.deleteByIds(tokenUser, productIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{productId}")
    public Result<ProductVO> findById(@PathVariable String productId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看产品权限校验

        return Result.success(
                productService.selectById(productId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<ProductListVO>> listAll(@Valid ProductQuery productQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看产品权限校验

        return Result.success(
                productService.select(productQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<ProductListVO>> page(@Valid ProductQuery productQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                productService.selectPage(productQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/selectByProductTypeId")
    public Result<Page<ProductListVO>> selectByProductTypeIdAndProductName(@Valid String productTypeId
            , String productName, Integer pageNo, Integer pageSize) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验


        Page<ProductDO> page = productService.selectByProductTypeIdAndProductName(productTypeId
                , productName, pageNo, pageSize, tokenUser.getSiteId());
        return Result.success(
                page.to(ProductListVO.class)
        );
    }


    @Override
    @GetMapping("/selectBySite")
    public Result<List<ProductListVO>> selectBySite() {
        TokenUser tokenUser = UserContext.getUser();

        // todo 查看项目权限校验
        List<ProductDO> productDOList = productService.selectBySite(tokenUser.getSiteId());
        // 是否设置成Banner
        List<ProductBannerDO> productBannerDOList = productBannerService.selectBySite(tokenUser.getSiteId());
        List<String> hadProductIds = productBannerDOList.parallelStream()
                .map(ProductBannerDO::getProductId).collect(Collectors.toList());
        return Result.success(
                productDOList.parallelStream().map(productDO -> {
                    ProductListVO productListVO = productDO.to(ProductListVO.class);
                    productListVO.setIsBanner(hadProductIds.contains(productDO.getId()));
                    return productListVO;
                }).parallel().collect(Collectors.toList())
        );
    }


}

